
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package be.ac.ua.pats.thesisrobotica.robot.RR.sensors;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.pats.thesisrobotica.robot.RR.Reflective;
import be.ac.ua.pats.thesisrobotica.robot.constants.RobotConstants;
import be.ac.ua.pats.thesisrobotica.robot.communication.Connection;
import be.ac.ua.pats.thesisrobotica.robot.RR.sensors.utils.SensorNotification;

import lejos.nxt.ColorLightSensor;
import lejos.nxt.SensorPort;
import lejos.nxt.SensorPortListener;
import lejos.nxt.comm.RConsole;

/**
 *
 * @author Admin
 */
public class ColorSensor extends Sensor implements Reflective {

    ColorLightSensor sensor;
    private boolean connected;
   

    public ColorSensor() {
        this(null);
        connected = false;
        SensorPort.S1.addSensorPortListener(listener);
    }

    /**
     * @param connection The connection over which notifications should be sent
     */
    public ColorSensor(Connection connection) {
        super(connection);

        sensor = new ColorLightSensor(RobotConstants.SensorPorts.COLORSENSOR, ColorLightSensor.TYPE_COLORFULL);
        type = RobotConstants.Types.COLORSENSOR;

        connected = true;
    }

    /**
     * @return The current sensor reading
     */
    public String getValue() {
        int[] colorArray = sensor.getColor();
        return (String.valueOf(colorArray[0])+","+String.valueOf(colorArray[1])+","+String.valueOf(colorArray[2]));
    }
    
    /**
     * Constantly checks wether the sensor value has changed, taking into account the threshold and time interval,
     * if this happens the method will send a notification over the connection containg the sensor-ID and the value.
     */
    public void run() {
        RConsole.println("Colorsensor sensor running");

        String             previousValue      = null;
        String             currentValue       = null;
        sensorNotification = new SensorNotification(connection,id);

        int[] RGB;
        StringBuilder s1;

        if((threshold == 0) && (interval == 0)) {
            while (connection.isConnected()) {
                RGB = sensor.getColor();
                RConsole.println("Current color: "+RGB[0]+""+RGB[1]+""+RGB[2]);
                s1 = new StringBuilder();
                s1.append(String.valueOf(RGB[0])).append(String.valueOf(RGB[1])).append(String.valueOf(RGB[2]));
                currentValue = s1.toString();
                if (!currentValue.equals(previousValue)) {
                    sensorNotification.setAndSend(this.id, currentValue);

                    previousValue = currentValue;
                }
            }
        } else if((threshold != 0) && (interval == 0)) {
            RGB = sensor.getColor();
                RConsole.println("Current color: "+RGB[0]+""+RGB[1]+""+RGB[2]);


        }

        RConsole.println("Colorsensor stopped running");
    }

    @Override
    public void start() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
